package backend.component;

import backend.Instruction.RISCVinst;
import midend.IR_Value.IR_BaseBlock;
import midend.IR_Value.IR_Value;

import java.util.*;

public class RISCVblock extends RISCVcomponent{
    private String name;
    private LinkedList<RISCVinst> instructionlist;
    private List<RISCVblock> prelist,nextlist;
    private int depth;

    public RISCVblock(String name, IR_BaseBlock bb){
        this.name = name;
        this.instructionlist = new LinkedList<>();
        this.prelist = new ArrayList<>();
        this.nextlist = new ArrayList<>();
        setDepth(bb);
    }

    public void setInstructionlist(LinkedList<RISCVinst> instructionlist) {
        this.instructionlist = instructionlist;
    }

    private void setDepth(IR_BaseBlock bb){
        this.depth = bb.getLoopDep();
    }

    public int getdepth() {
        return depth;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public LinkedList<RISCVinst> getInstructionlist() {
        return instructionlist;
    }

    public void addInstructionlist(RISCVinst i1){
        instructionlist.add(i1);
    }

    public void addInstructionlistatindex(RISCVinst i1,int index){
        instructionlist.add(index,i1);
    }

    public void addInstructionlistatHead(RISCVinst i1){
        instructionlist.addFirst(i1);
    }

    public void setPreNxtBlock(HashMap<IR_Value,RISCVcomponent> map,List<IR_BaseBlock> pre,List<IR_BaseBlock> nxt){
        pre.forEach(block->this.prelist.add((RISCVblock) map.get(block)));
        nxt.forEach(block->this.nextlist.add((RISCVblock) map.get(block)));
    }

    public List<RISCVblock> getPrelist() {
        return prelist;
    }

    public List<RISCVblock> getNextlist() {
        return nextlist;
    }
    public void insertAfter(RISCVinst inst,RISCVinst nextinst){
        int id = instructionlist.indexOf(inst);
        instructionlist.add(id + 1,nextinst);
    }
    public void insertBefore(RISCVinst inst,RISCVinst nextinst){
        int id = instructionlist.indexOf(inst);
        instructionlist.add(id,nextinst);
    }
    public void deleteInstr(RISCVinst inst){
        instructionlist.remove(inst);
    }

    public RISCVinst findInstrbefore(RISCVinst inst){
        int id = instructionlist.indexOf(inst);
        return instructionlist.get(id - 1);
    }
}
